package com.example.myapplication.module3

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView.VERTICAL
import com.example.myapplication.R
import com.example.myapplication.databinding.FragmentCashBankBinding
import com.example.myapplication.entity.ItemBean
import com.example.myapplication.module1.ItemGridSpanSizeLookup
import com.example.myapplication.module1.adapter.CategoryContentAdapter
import com.example.mylibrary.base.BaseFragment
import com.example.mylibrary.utils.UiTools


class CashBankFragment : BaseFragment<FragmentCashBankBinding>() {

    override fun getBinding(): FragmentCashBankBinding {
        return FragmentCashBankBinding.inflate(layoutInflater)
    }

    private val mutableList = mutableListOf<Any>()
    private val itemBeanList = mutableListOf<ItemBean>()

    private lateinit var cashBank: Array<String>

    private val categoryContentAdapter: CategoryContentAdapter? by lazy {
        activity?.let { CategoryContentAdapter(it) }
    }

    override fun initView(view: View) {

        mutableList.clear()
        cashBank = UiTools.getStringArray(R.array.cashBank)
        val cashBankImage = UiTools.resources.obtainTypedArray(R.array.cashBankImage)

        val gridLayoutManager = GridLayoutManager(activity, 4, VERTICAL, false)

        for (index in cashBank.indices) {
            itemBeanList.clear()
            itemBeanList.add(
                ItemBean(
                    3,
                    0,
                    index,
                    "30$index",
                    cashBank[index],
                    cashBankImage.getResourceId(index, 0)
                )
            )
            mutableList.addAll(itemBeanList)
        }
        cashBankImage.recycle()
        gridLayoutManager.spanSizeLookup = ItemGridSpanSizeLookup(mutableList, 4)
        viewBinding.rvList.layoutManager = gridLayoutManager
        viewBinding.rvList.adapter = categoryContentAdapter

        categoryContentAdapter?.mutableList = mutableList
    }

}
